package com.valveinspectionsys.maintain.activity;

import java.util.List;

import com.valveinspectionsys.R;
import com.valveinspectionsys.dbhelp.XmlNameHelper;
import com.valveinspectionsys.httpserver.RequestUrl;
import com.valveinspectionsys.httpserver.UploadItem;
import com.valveinspectionsys.httpserver.UploadManager;
import com.valveinspectionsys.maintain.db.DeviceMaintainDao;
import com.valveinspectionsys.routing.activity.DeviceNavigationUploadActivity;
import com.valveinspectionsys.routing.db.DeviceRoutingDao;
import com.valveinspectionsys.util.SharedPreferenceUtil;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 保养图文上传
 * @author King
 *
 */
public class TuwenUploadActivity extends Activity{
    public Button button;// 上传按钮

    public TextView textview1;// 需上传总数

    public TextView textview2;// 未上传总数

    public TextView textview3;// 进度条描述

    public TextView textview4;

    public ProgressBar pb;// 进度条
    
    public DeviceMaintainDao  dao;
    
    public SharedPreferenceUtil sp;
    
    public int total=0;
    public int noupload=0;
    public List<UploadItem> list;//所有上传数据
    public UploadManager  upmanager;//上传管理类
    public boolean isUpload=false;
    public RequestUrl  r;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	
	super.onCreate(savedInstanceState);
	setContentView(R.layout.device_onekey_upload);
	r=new RequestUrl(this);
	sp=new SharedPreferenceUtil(this);
	dao=new DeviceMaintainDao(this, sp.getValue(XmlNameHelper.SqliteName));
	upmanager=new UploadManager(this,myHandler, dao.getDb());
	button = (Button) findViewById(R.id.waterbanding_upload_button);
	pb = (ProgressBar) findViewById(R.id.waterbanding_progressBar);
	textview1 = (TextView) findViewById(R.id.waterbanding_upload_total);
	textview2 = (TextView) findViewById(R.id.waterbanding_noupload_total);
	textview3 = (TextView) findViewById(R.id.waterbanding_pb_textview);
	textview4 = (TextView) findViewById(R.id.onekeyupload_info);

	textview4.setText("设备图文数据一键上传");

	noupload=dao.queryNoUploadTw();
	total=dao.queryUploadTw();
	textview1.setText(""+total);
	textview2.setText(noupload+"");
	pb.setMax(noupload);
	button.setOnClickListener(new myClickListener());
    }
    
    class myClickListener implements OnClickListener{

	@Override
	public void onClick(View v) {
	       
	        list=dao.queryNoUploadTuwen(sp.getValue(XmlNameHelper.IMEI));
	        if(list==null||list.size()==0){
	           
	            return;
	        }
	        upmanager.start();
	        for(int i=0;i<list.size();i++){
	           
	            upmanager.addTask(r.getUrl(list.get(i).getUrl()), list.get(i));
	        }
	        
	    
	}
	
    }
    
    Handler  myHandler=new Handler(){

	@Override
	public void handleMessage(Message msg) {
	    if(msg.what==1){
		pb.setProgress(pb.getProgress()+1);
		noupload--;
		textview2.setText(noupload+"");
		textview3.setText("正在上传中..."+100*(float)pb.getProgress()/pb.getMax()+"%");
		
		
		if(pb.getProgress()==pb.getMax()){
		    
		    Toast.makeText(TuwenUploadActivity.this, "上传完毕", Toast.LENGTH_SHORT).show();
		    textview3.setText("上传完毕");
		    upmanager.stop();
		    isUpload=false;
		}
		
	    }else if(msg.what==0){
		 Toast.makeText(TuwenUploadActivity.this, "上传错误", Toast.LENGTH_SHORT).show();
		    upmanager.stop();
		    isUpload=false;
		
	    }
	    super.handleMessage(msg);
	}
	
    };
    
    
    
    
    

    @Override
    protected void onDestroy() {
	dao.close();
	if(isUpload){
	    upmanager.stop();
	}
	super.onDestroy();
    }
             
    
}
